<template>
  <div>
    <div class="add">
      <div class="add-main">
        <div class="add-btn">
          <button @click="back()">返回</button>
        </div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="add-content"
          v-loading="loading"
        >
          <el-form-item label="轮播名称" prop="name">
            <el-input style="width: 400px" v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="所在分区" prop="partitionId">
            <el-select v-model="ruleForm.partitionId" placeholder="请选择分区">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="轮播图片" prop="imgUrl">
            <v-uploadImg :isimgurl="ruleForm.imgUrl" @imgsuccess="imgsuccess" />
          </el-form-item>

          <el-form-item label="轮播链接" prop="url">
            <el-input style="width: 400px" v-model="ruleForm.url"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { admincarouseladd, admincarouselupdate } from "@/api/index";
import uploadImg from "@/component/uploadImg/index.vue";
export default {
  components: {
    "v-uploadImg": uploadImg,
  },
  data() {
    return {
      loading: false,
      options: [
        {
          value: 1,
          label: "首页",
        },
        {
          value: 2,
          label: "案例区",
        },
        {
          value: 3,
          label: "成片区",
        },
        {
          value: 4,
          label: "素材区",
        },
        {
          value: 5,
          label: "任务区",
        },
      ],
      ruleForm: {},
      rules: {
        name: [{ required: true, message: "请输入轮播名称", trigger: "blur" }],
        partitionId: [
          { required: true, message: "请选择分区", trigger: "change" },
        ],
        imgUrl: [{ required: true, message: "请添加图片", trigger: "blur" }],
      },
      isedit: false,
    };
  },
  created() {
    if (this.$route.query.obj) {
      this.isedit = true;
      this.ruleForm = JSON.parse(this.$route.query.obj);
    }
  },
  methods: {
    imgsuccess(imgUrl) {
      this.ruleForm.imgUrl = imgUrl;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid && !this.loading) {
          this.loading = true;
          if (this.isedit) {
            this.admincarouselupdate();
          } else {
            this.admincarouseladd();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm.imgUrl = "";
    },
    admincarouselupdate() {
      admincarouselupdate(this.ruleForm)
        .then((res) => {
          this.$message.success("编辑成功");
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
        });
    },
    admincarouseladd() {
      admincarouseladd(this.ruleForm)
        .then((res) => {
          this.$message.success("添加成功");
          this.resetForm("ruleForm");
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
        });
    },
    back() {
      this.$router.push({
        path: "/Rotationsettings",
      });
    },

    handleRemove(file) {
      console.log(file);

      this.picShow = false;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
  mounted() {
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#f2f2f2;");
  },

  beforeDestroy() {
    document.querySelector("body").removeAttribute("style");
  },
};
</script>

<style lang="scss" scoped>
.add {
  width: 100%;
  margin-top: 20px;
}

.add-main {
  width: 1200px;
  min-width: 1200px;
  height: 697px;
  margin: 0 auto;
  background-color: white;

  .add-btn {
    width: 77px;
    height: 40px;
    margin-top: 27px;
    margin-right: 32px;
    float: right;

    button {
      width: 77px;
      height: 40px;
      background-color: white;
      outline: none;
      cursor: pointer;
      border-radius: 5px;
      border: 1px solid gray;
    }
  }

  .add-content {
    padding-top: 70px;
  }
  .add-name {
    display: flex;
    padding-top: 25px;
    padding-left: 70px;

    .add-user {
      margin-right: 17px;
      margin-top: 7px;

      span {
        display: inline-block;
        padding-top: 1px;
        font-size: 14px;
      }
    }

    .add-input {
      width: 414px;

      input {
        width: 414px;
        padding-top: 3px;
        padding-bottom: 3px;
        padding-left: 5px;
        outline: none;
        border: 1px solid rgba(215, 215, 215, 1);
        border-radius: 3px;
      }
    }

    .add-select {
      width: 414px;

      input {
        width: 414px;

        padding-left: 5px;
        outline: none;
        border: 1px solid rgba(215, 215, 215, 1);
        border-radius: 3px;
      }
    }
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>